package jmine.tec.web.wicket.pages.example.monitor.table;

import images.ImageResources;

import java.util.List;

import jmine.tec.report.impl.table.CellValueResolver;
import jmine.tec.report.impl.table.ReportTableBuilder;
import jmine.tec.web.wicket.component.InlinedImage;
import jmine.tec.web.wicket.component.report.AbstractComponentValueResolver;
import jmine.tec.web.wicket.dao.WebTestEntityDAO;
import jmine.tec.web.wicket.domain.WebTestEntity;
import jmine.tec.web.wicket.pages.TableMonitor;

import org.apache.wicket.Component;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.markup.html.WebMarkupContainer;

import bancosys.tec.persist.dao.BaseDAOFactory;

/**
 * Exemplo de uma tableMonitor
 * 
 * @author nara.ribeiro
 * @created Jan 27, 2012
 */
public class TableMonitorExample extends TableMonitor<WebTestEntity>{

    @Override
    protected void addResultTableColumns(ReportTableBuilder<WebTestEntity> table) {
        table.addStringColumn("nome", "Nome", "nome");
        table.addStringColumn("mnemonico", "Mnemônico", "mnemonico");
        table.addDateColumn("data", "Data", "data");
        table.addNumberColumn("valor", "Valor", "valor", 2);
        table.addStringColumn("mnemonico2", "Par/Impar", new ParOuImparComponentValueResolver());
    }

    @Override
    protected List<WebTestEntity> doSearch(BaseDAOFactory daoFactory) {
        WebTestEntityDAO dao = daoFactory.getDAOByClass(WebTestEntityDAO.class);
        return dao.findAll();
    }

    @Override
    protected String getResultFileName() {
        return "Monitor Example";
    }

    private class ParOuImparComponentValueResolver extends AbstractComponentValueResolver<WebTestEntity> {

        public ParOuImparComponentValueResolver() {
            super(new CellValueResolver<WebTestEntity>() {
                public Object resolveCellValue(WebTestEntity rowValue) {
                    if (rowValue.getPk() % 2 == 0) {
                        return "Par";
                    } else {
                        return "Impar";
                    }
                }
            });
        }

        public Component createComponents(String componentId, WebTestEntity value) {
            if (value.getPk() % 2 == 0) {
                return new InlinedImage(componentId, new ResourceReference(ImageResources.class, "accept.png"));
            } else {
                return new InlinedImage(componentId, new ResourceReference(ImageResources.class, "error.png"));
            }
        }
    }

    /**
     * Não faz nada
     */
    @Override
    protected void addComponents(WebMarkupContainer container) {
        //Não faz nada
    }
}
